package com.xs.ahoi.hcxs.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import butterknife.ButterKnife;
import com.xs.ahoi.hcxs.R;
import com.xs.ahoi.hcxs.dao.BookDao;
import com.xs.ahoi.hcxs.dao.ConfigDao;
import com.xs.ahoi.hcxs.model.Book;
import com.xs.ahoi.hcxs.utils.Constants;
import com.xs.ahoi.hcxs.utils.FileUtil;

/**
 * author: cdfuhuichao
 * date: 2019/8/19 10:28
 * path: com.xs.ahoi.hcxs.ui.activity.WelcomeActivity
 * description: 启动页
 */
public class WelcomeActivity extends BaseActivity {

    private ConfigDao configDao;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.hideStatusBar();
        setContentView(R.layout.activity_welcome);
        ButterKnife.bind(this);
        init();
    }

    @Override
    protected void onResume() {
        super.onResume();
        openDefaultActivity();
    }

    private void init() {
        configDao = ConfigDao.getInstance();
        openDefaultActivity();
        getWindow().setBackgroundDrawable(null);
    }

    // 跳转到内容页面
    private void openDefaultActivity() {
        Intent intent;
        int bookId = configDao.getBookId();

        if (configDao.getOpenContentActivity() && bookId > 0) {
            // 验证书籍路径存不存在
            Book book = BookDao.getInstance().getById(bookId);
            if (book == null || !FileUtil.isFile(book.getPath())) {
                intent = new Intent(this, IndexActivity.class);
            } else {
                intent = new Intent(this, BookContentActivity.class);
                intent.putExtra(Constants.DEFAULT_ID, bookId);
            }
        } else {
            intent = new Intent(this, IndexActivity.class);
        }
        startActivity(intent);
        overridePendingTransition(R.anim.welcome_in, R.anim.welcome_out);
    }
}